﻿namespace BizTalk.ESB.Extensions.ItineraryOM
{
    using Microsoft.Practices.ESB.Exception.Management;
    using Microsoft.Practices.ESB.Itinerary;
    //using Microsoft.Practices.ESB.Itinerary.OM.V1.Properties;
    using Microsoft.Practices.ESB.Itinerary.OM.V2;
    using System;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Runtime.Serialization;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    [Serializable, XmlRoot(Namespace="http://schemas.microsoft.biztalk.practices.esb.com/itinerary", IsNullable=false), DebuggerStepThrough, DesignerCategory("code"), GeneratedCode("xsd", "2.0.50727.3038"), XmlType(AnonymousType=true, Namespace="http://schemas.microsoft.biztalk.practices.esb.com/itinerary")]
    public class Itinerary : IDeserializationCallback
    {
        private string beginTimeField;
        private ItineraryBizTalkSegment bizTalkSegmentField;
        private string completeTimeField;
        private bool isRequestResponseField;
        private bool isRequestResponseFieldSpecified;
        [NonSerialized, XmlIgnore]
        public IDictionary<string, ItineraryServicesService> itineraryDictionary;
        private string nameField;
        private ItineraryProperty[] propertyBagField;
        private ItineraryResolvers[] resolverGroupsField;
        private int servicecountField;
        private ItineraryServiceInstance serviceInstanceField;
        private ItineraryServices[] servicesField;
        private string stateField;
        private string uuidField;
        private string versionField;
        private string activityIdField;
        private string correlationIdField;

        internal void Initialize()
        {
            EventLogger.Write("Initializing itinerary services");
            this.servicecount = this.Services.Length;
            this.InitializeServiceInstance();
            this.itineraryDictionary = new Dictionary<string, ItineraryServicesService>();
            for (int i = 0; i < this.Services.Length; i++)
            {
                ItineraryServicesService service = this.Services[i].Service;
                EventLogger.Write("Adding itinerary services " + service.id);
                if (!string.IsNullOrEmpty(service.id))
                {
                    if (this.itineraryDictionary.ContainsKey(service.id))
                    {
                        this.itineraryDictionary[service.id] = service;
                    }
                    else
                    {
                        this.itineraryDictionary.Add(service.id, service);
                    }
                }
            }
        }

        private void InitializeServiceInstance()
        {
            ItineraryServices services = null;
            if (this.ServiceInstance == null)
            {
                services = this.Services[0];
                this.ServiceInstance = new ItineraryServiceInstance();
                this.ServiceInstance.isRequestResponse = services.Service.isRequestResponse;
                this.ServiceInstance.isRequestResponseSpecified = services.Service.isRequestResponseSpecified;
                this.ServiceInstance.name = services.Service.name;
                this.ServiceInstance.position = services.Service.position;
                this.ServiceInstance.positionSpecified = services.Service.positionSpecified;
                this.ServiceInstance.state = services.Service.state;
                this.ServiceInstance.type = services.Service.type;
                this.ServiceInstance.uuid = services.Service.uuid;
            }
            if ((this.ServiceInstance.position < 0) || (this.ServiceInstance.position >= this.Services.Length))
            {
                throw new ItineraryOMException(BizTalk.ESB.Extensions.ItineraryOM.Properties.Resources.ServiceInstancePositionInvalid, new object[] { this.ServiceInstance.position });
            }
            services = this.Services[this.ServiceInstance.position];
            if (services.Service.stageSpecified && !this.ServiceInstance.stageSpecified)
            {
                this.ServiceInstance.stage = services.Service.stage;
                this.ServiceInstance.stageSpecified = true;
            }
        }

        public void OnDeserialization(object sender)
        {
            this.Initialize();
        }

        [XmlAttribute]
        public string beginTime
        {
            get
            {
                return this.beginTimeField;
            }
            set
            {
                this.beginTimeField = value;
            }
        }

        [XmlElement(Form=XmlSchemaForm.Unqualified)]
        public ItineraryBizTalkSegment BizTalkSegment
        {
            get
            {
                return this.bizTalkSegmentField;
            }
            set
            {
                this.bizTalkSegmentField = value;
            }
        }

        [XmlAttribute]
        public string completeTime
        {
            get
            {
                return this.completeTimeField;
            }
            set
            {
                this.completeTimeField = value;
            }
        }

        [XmlAttribute]
        public bool isRequestResponse
        {
            get
            {
                return this.isRequestResponseField;
            }
            set
            {
                this.isRequestResponseField = value;
            }
        }

        [XmlIgnore]
        public bool isRequestResponseSpecified
        {
            get
            {
                return this.isRequestResponseFieldSpecified;
            }
            set
            {
                this.isRequestResponseFieldSpecified = value;
            }
        }

        [XmlAttribute]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        [XmlArrayItem("Property", Form=XmlSchemaForm.Unqualified, IsNullable=false), XmlArray(Form=XmlSchemaForm.Unqualified)]
        public ItineraryProperty[] PropertyBag
        {
            get
            {
                return this.propertyBagField;
            }
            set
            {
                this.propertyBagField = value;
            }
        }

        [XmlArrayItem("Resolvers", Form=XmlSchemaForm.Unqualified, IsNullable=false), XmlArray(Form=XmlSchemaForm.Unqualified)]
        public ItineraryResolvers[] ResolverGroups
        {
            get
            {
                return this.resolverGroupsField;
            }
            set
            {
                this.resolverGroupsField = value;
            }
        }

        [XmlAttribute]
        public int servicecount
        {
            get
            {
                return this.servicecountField;
            }
            set
            {
                this.servicecountField = value;
            }
        }

        [XmlElement(Form=XmlSchemaForm.Unqualified)]
        public ItineraryServiceInstance ServiceInstance
        {
            get
            {
                return this.serviceInstanceField;
            }
            set
            {
                this.serviceInstanceField = value;
            }
        }

        [XmlElement("Services", Form=XmlSchemaForm.Unqualified)]
        public ItineraryServices[] Services
        {
            get
            {
                return this.servicesField;
            }
            set
            {
                this.servicesField = value;
            }
        }

        [XmlAttribute]
        public string state
        {
            get
            {
                return this.stateField;
            }
            set
            {
                this.stateField = value;
            }
        }

        [XmlAttribute]
        public string uuid
        {
            get
            {
                return this.uuidField;
            }
            set
            {
                this.uuidField = value;
            }
        }

        [XmlAttribute]
        public string version
        {
            get
            {
                return this.versionField;
            }
            set
            {
                this.versionField = value;
            }
        }

        [XmlAttribute]
        public string activityId
        {
            get
            {
                return this.activityIdField;
            }
            set
            {
                this.activityIdField = value;
            }
        }

        [XmlAttribute]
        public string correlationId
        {
            get
            {
                return this.correlationIdField;
            }
            set
            {
                this.correlationIdField = value;
            }
        }
    }
}

